<template>
  <div class="table-box">
    <ProTable title="主题列表" ref="proTableRef" row-key="id" :columns="columns" highlight-current-row :request-api="loadData">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <ElButton type="primary" :icon="CirclePlus" @click="onAddClicked" v-auth="'ThemeCreate'">新增</ElButton>
      </template>
      <!-- 主题图标 -->
      <template #icon="scope">
        <ElIcon v-if="scope.row.icon" :size="18">
          <Component :is="scope.row.icon"></Component>
        </ElIcon>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <ElButton type="primary" link :icon="View" @click="onViewClicked(scope.row)" v-auth="'ThemeView'">查看</ElButton>
        <ElButton type="primary" link :icon="EditPen" @click="onEditClicked(scope.row)" v-auth="'ThemeUpdate'">编辑</ElButton>
        <ElButton type="primary" link :icon="Delete" @click="onDeleteClicked(scope.row)" v-auth="'ThemeDelete'">删除</ElButton>
      </template>
    </ProTable>
    <ThemeForm ref="formRef" @success="refresh" />
  </div>
</template>

<script setup lang="ts" name="ZLTheme">
import { ref } from 'vue'
import ProTable from '@/components/ProTable/index.vue'
import { ColumnProps, ProTableInstance } from '@/components/ProTable/interface'
import { Delete, CirclePlus, EditPen, View } from '@element-plus/icons-vue'
import ThemeForm from './theme-form.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Theme } from '@api/uc/theme/interface'
import { pageTheme, removeTheme } from '@api/uc/theme'
import { FormType } from '@/enums'
const proTableRef = ref<ProTableInstance>()
// 打开 drawer(新增、查看、编辑)
const formRef = ref<InstanceType<typeof ThemeForm> | null>(null)

function getIndex(index: number) {
  const { current, size } = proTableRef.value!.pageable
  return (current - 1) * size + index + 1
}

// 表格配置项
const columns: ColumnProps[] = [
  { type: 'index', label: '序号', width: 60, index: getIndex },
  { prop: 'name', label: '主题名称', align: 'left', search: { el: 'input' } },
  { prop: 'themeColor', label: '主题色', width: 150 },
  { prop: 'operation', label: '操作', fixed: 'right', width: 330 }
]

// 加载数据
function loadData(query: Theme.Query) {
  query.order = 'update_time desc'
  return pageTheme(query)
}

// 刷新列表
function refresh() {
  proTableRef.value?.getTableList()
}

// 添加主题
const onAddClicked = () => {
  formRef.value?.open(FormType.ADD)
}

// 查看菜单
const onViewClicked = (row: any) => {
  formRef.value?.open(FormType.VIEW, row.id)
}

// 编辑菜单
const onEditClicked = (row: any) => {
  formRef.value?.open(FormType.EDIT, row.id)
}

// 删除菜单
const onDeleteClicked = (row: any) => {
  ElMessageBox.confirm('您确定要删除该菜单吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      removeTheme(row.id).then(() => {
        ElMessage({
          type: 'success',
          message: '删除成功!'
        })
        proTableRef.value?.getTableList()
      })
    })
    .catch(() => {
      ElMessage({
        type: 'warning',
        message: '删除取消!'
      })
    })
}
</script>
